﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using BLL;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using System.Collections;

namespace CRM.GUI.QLKhachHang
{
    public partial class frmDuplicateReport : DevExpress.XtraEditors.XtraForm
    {
        DataTranferEntity data = new DataTranferEntity();
        public static LoadDeDup LoadDeDupList;
        public delegate void LoadDeDup(List<DataTable> dtDeDupList);
        public frmDuplicateReport()
        {
            InitializeComponent();
            grdViewDup.BestFitColumns();
            grdViewDup.OptionsView.ColumnAutoWidth = false;
            grdViewDup.ScrollStyle = ScrollStyleFlags.LiveHorzScroll | ScrollStyleFlags.LiveVertScroll;
            grdViewDup.HorzScrollVisibility = ScrollVisibility.Always;
            IntialLoad();
            frmBasicList.LoadDupList = new frmBasicList.LoadDup(LoadDupList);
            grdColcounter.UnboundType = DevExpress.Data.UnboundColumnType.Integer;
            grdViewDup.CustomUnboundColumnData += grdViewDup_CustomUnboundColumnData;
        }

        void grdViewDup_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
                e.Value = e.ListSourceRowIndex + 1;
        }
        DataTranferEntity trans;
        private DataTable _dtDupList;
        FilterFunc filter;
        private void IntialLoad()
        {
            trans = new DataTranferEntity();
            cmbCity.View.Columns.AddField("RegionName").Visible = true;
            cmbCity.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 2).ToList();
            cmbCity.DisplayMember = "RegionName";
            cmbCity.ValueMember = "Region_Code";
            cmbDistrict.View.Columns.AddField("RegionName").Visible = true;
            cmbDistrict.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 3).ToList();
            cmbDistrict.DisplayMember = "RegionName";
            cmbDistrict.ValueMember = "Region_Code";
            cmbWard.View.Columns.AddField("RegionName").Visible = true;
            cmbWard.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 4).ToList();
            cmbWard.DisplayMember = "RegionName";
            cmbWard.ValueMember = "Region_Code";
            cmbCollector.DataSource = SharedData.CollectorList;
            cmbCollector.DisplayMember = "Collector_Code";
            cmbCollector.ValueMember = "Collector_ID";
            cmbCurrentBrand.DataSource = SharedData.BrandList;
            cmbCurrentBrand.DisplayMember = "Brand_Code";
            cmbCurrentBrand.ValueMember = "Brand_ID";
            cmbPreviousBrand.DataSource = SharedData.BrandList;
            cmbPreviousBrand.DisplayMember = "Brand_Code";
            cmbPreviousBrand.ValueMember = "Brand_ID";

        }

        private void LoadDupList(DataTable dt)
        {            
            lblTotalDupCount.Text = dt.Rows.Count.ToString();
            _dtDupList = dt;
            grdDup.DataSource = _dtDupList;
        }
        

        private void itemDelete_Click(object sender, EventArgs e)
        {
            filter = new FilterFunc();
            int[] SelectedRow = grdViewDup.GetSelectedRows();
            List<DataRow> DeleteCus = new List<DataRow>();
            
            if (SelectedRow.Length > 0 && MessageBox.Show("Xóa những dòng này???", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                foreach (int i in SelectedRow)
                {
                    DeleteCus.Add(grdViewDup.GetDataRow(i));        //Get row by selected row
                }
                filter.DeleteDup(DeleteCus);                        //Delete dup in RawList
                foreach (DataRow row in DeleteCus)
                {
                    _dtDupList.Rows.Remove(row);
                }
            }
            else
            {
                //do nothing if user choose No
                return;
            }
            //filter again to refresh duplist
            filter.FilterDupList(filter.dtRaw);
            LoadDupList(filter.DupList);            
            lblTotalDupCount.Text = _dtDupList.Rows.Count.ToString();
        }

        private void frmDuplicateReport_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmDupcateDatabase frm = new frmDupcateDatabase();
            filter = new FilterFunc();
            DataTranferEntity trans = new DataTranferEntity();
            trans.Source.TotalDup = int.Parse(lblTotalDupCount.Text);
            trans.Source.TotalData = int.Parse(lblTotalListCount.Text);
            filter.FilterDeDupList(filter.dtRaw);
            LoadDeDupList(filter.DeDupList);
            frm.ShowDialog();
        }

        private void grdViewDup_RowUpdated(object sender, RowObjectEventArgs e)
        {
            grdViewDup.OptionsBehavior.Editable = false;

        }

        private void itemEdit_Click(object sender, EventArgs e)
        {
            grdViewDup.OptionsBehavior.Editable = true;
        }

        private void ExToXls_Click(object sender, EventArgs e)
        {
            grdViewDup.OptionsPrint.AutoWidth = false;
            grdViewDup.BestFitColumns();
            SaveFileDialog SaveDlg = new SaveFileDialog();
            SaveDlg.Filter = "*.xls|*.xlsx";
            SaveDlg.FileName = "Report.xlsx";
            if (SaveDlg.ShowDialog() != DialogResult.Cancel)
            {

             DevExpress.XtraPrinting.XlsxExportOptions opt = new DevExpress.XtraPrinting.XlsxExportOptions()
                {
                    TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text,
                    ExportMode = DevExpress.XtraPrinting.XlsxExportMode.SingleFile,

                    ShowGridLines = true,
                };
                grdViewDup.ExportToXlsx(SaveDlg.FileName.ToString(), opt);
            }

        }



    }
}